package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.CollectionDAO;
import com.diankangwo.dao.MessageDao;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.Collection;
import com.diankangwo.domain.Message;
import com.diankangwo.domain.UserInfo;
import com.diankangwo.util.PageSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "消息")
public class MessageController {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private CollectionDAO collectionDAO;

    /**
     * 按UID查询消息（包含系统消息）
     *
     * @param accessToken token
     * @param pageNo      页码
     * @param pageSize    页大小
     * @return
     */
    @ApiOperation(value = "按UID查询消息（包含系统消息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "分页参数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/message/findByuid", method = RequestMethod.POST)
    public String findByuid(String accessToken, int pageNo, int pageSize) {
        UserInfo user = userDAO.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        PageSupport<Message> mList = new PageSupport<>();
        if (user != null) {
            code = "1";
            mList = messageDao.findByUid(user.getUid(), pageNo, pageSize);
            if (mList != null) {
                msg = "未查询到消息！";
            } else {
                return resultMsg.mapToJson(code, msg, mList.getResultData());
            }
        } else {
            msg = "token已失效！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }


    /**
     * 按ID查询消息消息详情
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "按ID查询消息消息详情", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/message/findByid", method = RequestMethod.POST)
    public String findByid(int id) {
        String code = "2";
        String msg = "系统异常！";
        Message m = messageDao.find(id);
        m.setState(1);
        if (m != null) {
            code = "1";
            msg = "查询消息详情成功！";
        } else {
            code = "1";
            msg = "未查询到消息信息！";
        }
        return resultMsg.mapToJson(code, msg, m);
    }

    /**
     * 按uid查询用户是否有未读消息
     *
     * @param accessToken accessToken
     * @return
     */
    @ApiOperation(value = "按ID查询消息消息详情", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/message/findUidByState", method = RequestMethod.POST)
    public String findUidByState(String accessToken) {
        UserInfo user = userDAO.getUserByToken(accessToken);
        String code = "1";
        String msg = "";
        Map<String, Object> result = new HashMap<>();
        int num = 0;
        List<Message> m = messageDao.findByUid(user.getUid(), 1, Integer.MAX_VALUE).getResultData();
        for (int i = 0; i < m.size(); i++) {
            if (m.get(i).getState() == 0) {
                num++;
                m.get(i).setState(1);
                messageDao.update(m.get(i));
            }
        }
        PageSupport<Collection> collectionPageSupport = collectionDAO.getCollectionByUid(user.getUid(), 1, Integer.MAX_VALUE, null);
        result.put("collection", collectionPageSupport == null ? 0 : collectionPageSupport.getResultData().size());
        result.put("commissionbalance", user.getCommissionbalance());
        result.put("balance", user.getBalance());
        result.put("messageState", num);
        result.put("Integral", user.getIntegral());

        return resultMsg.mapToJson(code, msg, result);
    }


    /**
     * 按消息类型查询个人消息
     *
     * @param accessToken token
     * @param type        type
     * @param pageNo      页码
     * @param pageSize    页大小
     * @return
     */
    @ApiOperation(value = "按消息类型查询个人消息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNo", value = "分页参数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/message/findByType", method = RequestMethod.POST)
    public String findByType(String accessToken, String type, int pageNo, int pageSize) {
        UserInfo user = userDAO.getUserByToken(accessToken);
        String code = "2";
        String msg = "系统异常！";
        List<Message> mList = new ArrayList<>();
        if (user == null) {
            msg = "token已失效！";
        } else {
            JSONArray json = JSONArray.fromObject(type); // 首先把字符串转成 JSONArray 对象
            List l = JSONArray.toList(json);
            for (Object object : l) {
                List<Message> messageList = messageDao.findByType(user.getUid(), Integer.parseInt(object.toString()), pageNo, pageSize).getResultData();
                for (int j = 0; j < messageList.size(); j++) {
                    mList.add(messageList.get(j));
                }
            }
            if (mList != null) {
                code = "1";
                msg = "查询消息成功！";
                return resultMsg.mapToJson(code, msg, mList);
            } else {
                code = "1";
                msg = "未查询到消息信息！";
            }
        }
        return resultMsg.mapToJson(code, msg, null);
    }


}
